/**
 * 
 */
package com.syncshot.androidapp.activities;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.syncshot.androidapp.Constant;
import com.syncshot.androidapp.R;
import com.syncshot.androidapp.helper.WSHelper.Call;
import com.syncshot.androidapp.helper.WSHelper.WebserviceCaller;
import com.syncshot.androidapp.helper.WSHelper.WebserviceCaller.WebserviceCallerListener;
import com.syncshot.androidapp.model.Event;
import com.syncshot.androidapp.model.EventCreation;
import com.syncshot.androidapp.model.User;
import com.syncshot.androidapp.util.WSUtil;

/**
 * @author Stan
 * 
 */
public class CreateEventActivity extends AbstractActivity implements WebserviceCallerListener, OnSeekBarChangeListener {
    /** Used locally to tag Logs */
    @SuppressWarnings("unused")
    private static final String TAG                    = "CreateEventActivity";

    private ProgressDialog      mProgressDialog;
    private SeekBar             mSeekBar;
    private TextView            mTimeTextView;
    private BroadcastReceiver   mUSerBroadcastReceiver = new BroadcastReceiver() {
                                                           @Override
                                                           public void onReceive(Context context, Intent intent) {
                                                               updateUsersList();
                                                           }
                                                       };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_event);

        mSeekBar = (SeekBar) findViewById(R.id.time_seekbar);
        mTimeTextView = (TextView) findViewById(R.id.eventDurationTextView);
        mSeekBar.setOnSeekBarChangeListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        updateUsersList();

        registerReceiver(mUSerBroadcastReceiver, new IntentFilter(Constant.BROADCAST_NEW_USER));
    }

    
    public void onClickCreateEvent(View view) {
        mProgressDialog = ProgressDialog.show(this, null, getString(R.string.create_in_progress));

        Event event = new Event();
        event.setDescription(((EditText) findViewById(R.id.description_edittext)).getText().toString());
        event.setTimer((((SeekBar) findViewById(R.id.time_seekbar)).getProgress() + 15));
        event.setRadius(1);
        event.setLatitude(10);
        event.setLongitude(10);

        SharedPreferences settings = getSharedPreferences(Constant.PREF, 0);
        String user = settings.getString(Constant.PREF_USERNAME, null);
        event.setUser(user);

        (new WebserviceCaller(this)).execute(WSUtil.generateCallNewEvent(event));
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
        if (mUSerBroadcastReceiver != null) {
            unregisterReceiver(mUSerBroadcastReceiver);
        }
    }

    @Override
    public void onResult(Call call, Object result) {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }

        if (result != null && result instanceof EventCreation) {
            getSharedPreferences(Constant.PREF, Context.MODE_PRIVATE).edit().putString(Constant.PREF_OWNED_EVENT, ((EventCreation) result).getId()).commit();
            finish();
        } else if (result instanceof User[]) {
            ((TextView) findViewById(R.id.nb_user_textview)).setText(((User[]) result).length + " CAMERAS");
        }
    }

    @Override
    public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) {
        if (fromUser) {
            progress += 15;
            String strProgress;
            if (progress >= 10) {
                strProgress = String.valueOf(progress);
            } else {
                strProgress = "0" + progress;
            }
            mTimeTextView.setText("00:" + strProgress);
        }

    }

    private void updateUsersList() {
        WebserviceCaller ws = new WebserviceCaller(this);
        ws.execute(WSUtil.generateCallUserList("10", "10"));
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }
}
